package com.lzb.watermarkcamera.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.lzb.watermarkcamera.BaseAc;
import com.lzb.watermarkcamera.Content;
import com.lzb.watermarkcamera.R;
import com.lzb.watermarkcamera.basic.utils.SPUtil;
import com.lzb.watermarkcamera.bean.ResolutionBean;
import com.lzb.watermarkcamera.databinding.ActivitySettingBinding;
import com.lzb.watermarkcamera.utils.AppSpUtils;

public class SettingActivity extends BaseAc<ActivitySettingBinding> implements View.OnClickListener {

    private boolean isTakePhotoSoundOpen = false; // 拍照声音开关状态
    private boolean isReferenceLineOpen = false; // 参考线开关状态
    private boolean isSelfieMirrorOpen = false; // 自拍镜像开关状态

    @Override
    protected int onCreate() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initView() {
        mDataBinding.ivSettingBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.llPictureResolution.setOnClickListener(this);
        mDataBinding.ivTakePhotoSound.setOnClickListener(this);
        mDataBinding.ivReferenceLine.setOnClickListener(this);
        mDataBinding.ivSelfieMirror.setOnClickListener(this);
        mDataBinding.llWatermarkDirection.setOnClickListener(this);
        mDataBinding.llFeedback.setOnClickListener(this);
        mDataBinding.llAgreement.setOnClickListener(this);
        mDataBinding.llPrivacy.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.llPictureResolution) {//照片分辨率
            startActivity(PictureResolutionActivity.class);
        } else if (id == R.id.ivTakePhotoSound) {//拍照声音
            toggleTakePhotoSound();
        } else if (id == R.id.ivReferenceLine) {//参考线
            toggleReferenceLine();
        } else if (id == R.id.ivSelfieMirror) {//自拍镜像
            toggleSelfieMirror();
        } else if (id == R.id.llFeedback) {//意见反馈
            startActivity(FeedbackActivity.class);
        } else if (id == R.id.llAgreement) {//用户协议
            Intent intent = new Intent(this, WebActivity.class);
            intent.putExtra("url", "https://www.baidu.com");
            intent.putExtra("title", getString(R.string.user_agreement));
            startActivity(intent);
        } else if (id == R.id.llPrivacy) {//隐私政策
            Intent intent = new Intent(this, WebActivity.class);
            intent.putExtra("url", "https://www.baidu.com");
            intent.putExtra("title", getString(R.string.privacy_policy));
            startActivity(intent);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        ResolutionBean resolutionBean = AppSpUtils.getResolution();
        mDataBinding.tvPictureResolution.setText(resolutionBean.getSize());

        // 获取拍照声音设置
        isTakePhotoSoundOpen = SPUtil.getBoolean(mContext, Content.isOpenTakeSound, false);
        updateTakePhotoSoundUI();

        // 获取参考线设置
        isReferenceLineOpen = SPUtil.getBoolean(mContext, Content.isOpenReferenceLine, false);
        updateReferenceLineUI();

        // 获取自拍镜像设置
        isSelfieMirrorOpen = SPUtil.getBoolean(mContext, Content.isOpenSelfieMirror, false);
        updateSelfieMirrorUI();
    }


    // 切换拍照声音开关
    private void toggleTakePhotoSound() {
        isTakePhotoSoundOpen = !isTakePhotoSoundOpen;
        SPUtil.putBoolean(mContext, Content.isOpenTakeSound, isTakePhotoSoundOpen);
        updateTakePhotoSoundUI();
    }

    // 更新拍照声音UI
    private void updateTakePhotoSoundUI() {
        if (isTakePhotoSoundOpen) {
            mDataBinding.ivTakePhotoSound.setImageResource(R.drawable.akai); // 开启状态图标
        } else {
            mDataBinding.ivTakePhotoSound.setImageResource(R.drawable.aguan); // 关闭状态图标
        }
    }

    // 切换参考线开关
    private void toggleReferenceLine() {
        isReferenceLineOpen = !isReferenceLineOpen;
        SPUtil.putBoolean(mContext, Content.isOpenReferenceLine, isReferenceLineOpen);
        updateReferenceLineUI();
    }

    /**
     * 更新参考线UI
     */
    private void updateReferenceLineUI() {
        mDataBinding.ivReferenceLine.setImageResource(isReferenceLineOpen ? R.drawable.akai : R.drawable.aguan);
    }

    // 切换自拍镜像开关
    private void toggleSelfieMirror() {
        isSelfieMirrorOpen = !isSelfieMirrorOpen;
        SPUtil.putBoolean(mContext, Content.isOpenSelfieMirror, isSelfieMirrorOpen);
        updateSelfieMirrorUI();
    }

    // 更新自拍镜像UI
    private void updateSelfieMirrorUI() {
        if (isSelfieMirrorOpen) {
            mDataBinding.ivSelfieMirror.setImageResource(R.drawable.akai); // 开启状态图标
        } else {
            mDataBinding.ivSelfieMirror.setImageResource(R.drawable.aguan); // 关闭状态图标
        }
    }

}